package com.polaris.live.popup

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.RechargeBeansToDiamondsAdapter
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.PopupRechargeBeansDiamondsBinding
import com.polaris.live.net.payerService
import com.polaris.live.resp.back_resp.RechargeCoinPurchasePriceResp
import com.polaris.live.resp.go_resp.RechargeCoinPurchaseRequest
import com.polaris.live.translation.R.string

/**
 *  Beans兑换Diamonds
 * @author Created by 天晴 on 2024/11/15 19:18
 * @since 1.0.0
 **/
class RechargeBeansToDiamondsPopup(
    context: Context,
    val mViewModel: BaseViewModel,
    val data: RechargeCoinPurchasePriceResp,
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupRechargeBeansDiamondsBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_recharge_beans_diamonds
    }

    private val mAdapter by lazy { RechargeBeansToDiamondsAdapter() }

    var gainCoins = 0L

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupRechargeBeansDiamondsBinding.bind(popupImplView)
        ViewUtils.setMargin(mBinding.bottomView, 0, 0, 0, NavigationBarUtils.getNavigationBarHeight(context))
        gainCoins = data.gainCoin
        setBeansTv()
        initRecycler()
        initListener()

    }

    private fun setBeansTv() {
        mBinding.beansCount.text =
            StringUtils.getString(string.availableBalance) + ": $gainCoins"
    }

    private fun initListener() {
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = mAdapter.getItemOrNull(position)
            when (view.id) {
                R.id.beans_ll -> {
                    item?.let {
                        purchase(it.coinPurchaseId)
                    }
                }
            }

        }

    }

    /**
     * 使用装饰
     */
    private fun purchase(
        priceId: String,
    ) {
        mViewModel.requestLoading({ payerService.purchase(RechargeCoinPurchaseRequest(priceId)) }, {
            gainCoins = it.gainCoin
            setBeansTv()
            ToastGravity.showGravity(string.diamond_exchange_success)
        }, {
            ToastGravity.showGravity(it.errorMsg)
        }, waitTime = 0)
    }

    private fun initRecycler() {
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }
        mAdapter.setList(data.priceList)
    }

    companion object {

        fun show(
            context: Context,
            mViewModel: BaseViewModel,
            data: RechargeCoinPurchasePriceResp,
        ) {
            XPopup.Builder(context)
                .isViewMode(true)
                .hasShadowBg(true)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .enableDrag(false)
                .asCustom(RechargeBeansToDiamondsPopup(context, mViewModel, data))
                .show()
        }
    }
}